<template>
	<view class="container">
		<input v-model ="userName" class="input" placeholder="请输入用户名"></input>
		<input v-model = "password"  class="input" password="True" placeholder="请输入密码"></input>
		<button class="button" @click="onLoginButtonClicked()">登录</button>
		<button class="button" @click="onRegisterButtonClicked()">注册</button>
	</view>
</template>

<script>
		
		let tag = "login";
		
		export default {
			data() {
				return {
					// 用户名
					userName: "",
					// 密码
					password: "",
				}
			},
		methods: {
			onRegisterButtonClicked() {
				this.$log.info(tag, "onRegisterButtonClicked");
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			onLoginButtonClicked(){
				this.$log.info(tag, "onLoginButtonClicked")
				if(this.userName!="" && this.password!=""){
					this.$log.info(tag, "not null check pass")
				}
				else{
					this.$log.error(tag, "用户名和密码不可以为空")
					return
				}
				uni.request({
					url: `${this.constant.BASE_URL}/api/customer/login`,
					method:'POST',
					data:{
						"password": this.password,  
						"username": this.userName
					},
					success: (res) => {
						this.$log.info(tag, "success");
						this.$log.info(tag, JSON.stringify(res.data))
						if(res.data.code == 200){
							this.loginManager.setToken(res.data.token)
							uni.showToast({
								icon:'success',
								title:"登录成功"
							})
							uni.navigateTo({
								url:"/pages/product-list/product-list"
							})
						}
						else{
							uni.showToast({
								icon:'fail',
								title:`${res.data.msg}`
							})
						}
					},
					fail: (err) => {
						this.$log.error(tag, err.errMsg)
						uni.showToast({
							icon:'fail',
							title:`${err.errMsg}`
						})
					}
				})
			},
			
		}
	}
</script>

<style>
	.container {
		padding: 20px 20px;
		height: 50vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.input {
		width: 100%;
		border-style: solid;
		border-color: grey;
		border-radius: 20px;
		border-width: 1px;
		padding: 5px 10px;
		font-size: 35rpx;
		margin-bottom: 10px;
	}
	
	.button {
		width: 100%;
		background-color: #08f;
		font-size: 35rpx;
		color: white;
		font-weight: bold;
		border-radius: 20px;
		margin-bottom: 10px;
	}
</style>

